<template>
  <!-- 添加或修改报工列对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="工单任务数量" prop="count">
                <el-input-number v-model="form.count" disabled placeholder="请输入工单任务数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="不合格数量" prop="errorCount">
                <el-input-number v-model="form.errorCount" placeholder="请输入不合格数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addAction, getById, updateAction } from '@/api/manage';
  import moment from 'moment';

  export default {
    name: "ZhijianGongxuForm",
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          errorCount: [
            { required: true, message: '不合格数量不能为空', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                if (value > this.form.count) {
                  callback(new Error('不合格数量不能大于总数量'));
                } else {
                  callback();
                }
              },
              trigger: 'blur'
            }
          ],
          /*remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],*/
        },
        url: {
          getById:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/',
          addBaogong:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/addBaogong',
          editZhijian:'/goodsSaledoGongxuUserMoney/goodsSaledoGongxuUserMoney/editZhijian',
        },
      };
    },
    created() {

    },
    methods: {
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          xuhao: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          doTime: null,
          userId: null,
          userName: null,
          money: null,
          count: null,
          allMoney: null,
          parentId: null,
          successCount: null,
          errorCount: null,
          successLv: null,
          status: "0",
          zhijianDate: null,
          zhijianUser: null,
          zhijianId: null
        };
        this.resetForm("form");
      },
      /** 新增按钮操作 */
      baogongShow(row) {
        this.reset();
        this.form.money=row.money;
        this.form.parentId=row.id;
        this.open = true;
        this.title = "添加报工";
      },
      baogongCount() {
        let that = this;
        if (that.form.money && that.form.count) {
          let totlePrice = this.functionNumberMul(that.form.count, that.form.money);
          that.$set(that.form, "allMoney", totlePrice);
        }
      },
      functionNumberMul(arg1, arg2) {
        var m = 0;
        var s1 = arg1.toString();
        var s2 = arg2.toString();
        try {
          m += s1.split(".")[1].length;
        } catch (e) {
        }
        try {
          m += s2.split(".")[1].length;
        } catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
      },
      /** 修改按钮操作 */
      zhijianShow(row) {
        this.reset();
        const id = row.id || this.ids;
        getById(this.url.getById, id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "质检";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAction(this.url.editZhijian, this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit('ok');
              });
            } else {
              addAction(this.url.addBaogong, this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$emit('ok');
              });
            }
          } else {
            this.$modal.msgError('表单校验失败，请检查输入');
            return false;
          }
        });
      },
    }
  };
</script>
